#迭代器实现斐波拉契数列
class Fib():
    def __init__(self):
        self.pre = 0
        self.cur = 1

    def __iter__(self):
        return self

    def __next__(self):
        result = self.cur
        self.pre, self.cur = self.cur, self.cur+self.pre
        return result

f = Fib()   # 注：进行实例化对象
print([next(f) for i in range(20)])
#结果为 [1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144, 233, 377, 610, 987, 1597, 2584, 4181, 6765]


#自定义异常类##########
class ListError(Exception):
    def __init__(self,list, message):  #重写Exception异常类的__init__方法
        self.list = list		# 注：目的是重写输出信息
        self.message = message
    # pass
    def __str__(self):   #重写Exception的str方法，获得异常类信息
        msg = "list:"+str(self.list)
        msg = msg +"\n" +self.message
        return msg
#print(ListError())
try:
    lst = [1,2,3,4,5,6,7,8,9,0,11]
    if not 8<len(lst)<10 :
        raise ListError(lst,"长度不在8-10之间")
        # raise ListError(lst,"长度不在8-10之间","xx","yy")
except ListError as  e:
    print("listError error:",e)
#结果为 listError error: list:[1, 2, 3, 4, 5, 6, 7, 8, 9, 0, 11]
#长度不在8-10之间

#结果为 listError error: ([1, 2, 3, 4, 5, 6, 7, 8, 9, 0, 11], '长度不在8-10之间', 'xx', 'yy')
